package org.example.spring_book.service;

import org.example.spring_book.constant.Constants;
import jakarta.servlet.http.HttpSession;
import org.example.spring_book.mapper.UserMapper;
import org.example.spring_book.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserService {
    //注入UserMapper
    @Autowired
    private UserMapper userMapper;

    public Boolean checkUserAndPassword(String username, String password, HttpSession session) {
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userMapper.selectUser(username);
        if (userInfo == null) {
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            //账号密码是对的
            //session的内容,取决于后面需要从session中获取什么
            session.setAttribute(Constants.SESSION_USER_KEY,username);
            return true;
        }
        return false;
    }
}
